package com.example.yyy

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.activity_app_bar.*
import kotlinx.android.synthetic.main.list_layout.*

class AppBarActivity : AppCompatActivity() {

    private val yearArray = arrayOf("shu","niu","hu","tou","long","she","ma","yang","huo","ji","guo","zhu")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_app_bar)
        //设置标题栏
        setSupportActionBar(tl_ab_title)
        //添加RecyclerView管理器
        rv_ab_main.layoutManager = LinearLayoutManager(this)
        //添加适配器
        rv_ab_main.adapter = AppBarAdapter(this,yearArray)
    }

    /**
     * RecyclerView适配器
     * @param context 上下文
     * @param infos 需要显示的内容
     */
    class AppBarAdapter(context: Context,private val infos:Array<String>):RecyclerBaseAdapter<RecyclerView.ViewHolder>(context){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view: View? = inflater.inflate(R.layout.list_layout,parent,false)
            return ItemHolder(view)
        }

        class ItemHolder(override val containerView: View?): RecyclerView.ViewHolder(containerView!!),
            LayoutContainer {
            fun bind(year: String){
                tv_lt_planet.text = year
            }
        }

        override fun getItemCount(): Int = infos.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
           ( holder as ItemHolder).bind(infos[position])
        }

    }
}